﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SLControlLibrary.Resources.Controls.Calendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.BusinessObjects;

namespace SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar
{
    public class ucDay : ucCalendarImage
    {
        #region Dependency Variables
        private static readonly DependencyProperty BorderTopFillProperty = DependencyProperty.Register("BorderTopFill", typeof(Brush), typeof(ucDay), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        private static readonly DependencyProperty BorderBottomFillProperty = DependencyProperty.Register("BorderBottomFill", typeof(Brush), typeof(ucDay), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        private static readonly DependencyProperty TextForegroundProperty = DependencyProperty.Register("TextForeground", typeof(Brush), typeof(ucDay), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
        private static readonly DependencyProperty BorderTopClipProperty = DependencyProperty.Register("BorderTopClip", typeof(PathGeometry), typeof(ucDay), new PropertyMetadata(new PathGeometry()));
        #endregion

        #region Dependency Properties
        public Brush BorderTopFill 
        {
            get { return (Brush)this.GetValue(BorderTopFillProperty); } 
            set { SetValue(BorderTopFillProperty, value); } 
        }
        public Brush BorderBottomFill 
        {
            get { return (Brush)this.GetValue(BorderBottomFillProperty); } 
            set { SetValue(BorderBottomFillProperty, value); } 
        }
        public Brush TextForeground
        {
            get { return (Brush)this.GetValue(TextForegroundProperty); }
            set { SetValue(TextForegroundProperty, value); }
        }
        public PathGeometry BorderTopClip
        {
            get { return (PathGeometry)this.GetValue(BorderTopClipProperty); }
            set { SetValue(BorderTopClipProperty, value); }
        } 
        #endregion

        #region Variables
        private int _Value = 0;
        private DaySelection _TopSelection = null;
        private DaySelection _BottomSelection = null;
        private bool _IsSelected = false;
        #endregion

        #region Properties
        public int Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                if (value > 0)
                {
                    if (IsWeekHeader)
                    {
                        base.ShortDesc = Enum.Parse(typeof(Enumerations.DayOfWeekShortDesc), value.ToString(), false).ToString();
                        base.LongDesc = Enum.Parse(typeof(Enumerations.DayOfWeekLongDesc), value.ToString(), false).ToString();
                    }
                    else
                        base.ShortDesc = value.ToString();
                }
                else
                    base.ShortDesc = string.Empty;

                base.DescToUse = Enumerations.BaseDescToUse.Short;
            }
        }
        public DaySelection TopSelection
        {
            get { return _TopSelection; }
            set
            {
                _TopSelection = value;
                BorderTopFill = _TopSelection.Color;
            }
        }
        public DaySelection BottomSelection
        {
            get { return _BottomSelection; }
            set
            {
                _BottomSelection = value;
                BorderBottomFill = _BottomSelection.Color;
            }
        }
        public bool IsSelected 
        {
            get { return _IsSelected; }
            set
            {
                _IsSelected = value;

                if (_IsSelected)
                {
                    BorderTopFill = new SolidColorBrush(Colors.Orange);
                    BorderBottomFill = new SolidColorBrush(Colors.Orange);
                }
                else
                {
                    BorderTopFill = new SolidColorBrush(Colors.White);
                    BorderBottomFill = new SolidColorBrush(Colors.White);
                }
            }
        }
        public bool IsWeekHeader { get; set; }
        public bool IsFirstInRange { get; set; }
        public bool IsLastInRange { get; set; }
        #endregion

        #region Event Variables
        public event MouseButtonEventHandler CalendarDayButtonMouseDown;
        public event MouseButtonEventHandler CalendarDayButtonMouseUp;
        #endregion

        #region Constructor
        public ucDay()
        {
            DefaultStyleKey = typeof(ucDay);

            BorderTopClip = DefaultClipPath();
            Loaded += new RoutedEventHandler(ucDay_Loaded);
        }
        #endregion

        #region Events
        #region ucDay_Loaded
        private void ucDay_Loaded(object sender, RoutedEventArgs e)
        {
            Rectangle brdHalfDayTop = (Rectangle)base.GetTemplateChild("brdHalfDayTop");

            if (brdHalfDayTop == null) return;

            PathGeometry pGeo = new PathGeometry();
            PathFigure pFig = new PathFigure();
            pFig.IsClosed = true;
            pFig.StartPoint = new Point(0, 0);

            LineSegment lSeg1 = new LineSegment();
            LineSegment lSeg2 = new LineSegment();

            if (IsFirstInRange && TopSelection.HalfDay)
            {
                lSeg1.Point = new Point(brdHalfDayTop.ActualWidth, 0);
                lSeg2.Point = new Point(0, this.ActualHeight);
                pFig.Segments.Add(lSeg1);
                pFig.Segments.Add(lSeg2);
            }
            else if (IsLastInRange && TopSelection.HalfDay)
            {
                lSeg1.Point = new Point(brdHalfDayTop.ActualWidth, 0);
                lSeg2.Point = new Point(0, this.ActualHeight);
                pFig.Segments.Add(lSeg2);
                pFig.Segments.Add(lSeg1);
            }            

            pGeo.Figures.Add(pFig);

            BorderTopClip = pGeo;

            if (DisplayMode == Enumerations.DisplayMode.Selection)
            {
                this.Cursor = Cursors.Hand;
                brdHalfDayTop.Fill = new SolidColorBrush(Colors.White);
            }
        }
        #endregion

        #region OnMouseEnter
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            if (DisplayMode == Enumerations.DisplayMode.Selection && !IsSelected)
            {
                BorderTopFill = new SolidColorBrush(Colors.Cyan);
                BorderBottomFill = new SolidColorBrush(Colors.Cyan);
            }            
        }
        #endregion

        #region OnMouseLeave
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            if (DisplayMode == Enumerations.DisplayMode.Selection && !IsSelected)
            {
                BorderTopFill = new SolidColorBrush(Colors.White);
                BorderBottomFill = new SolidColorBrush(Colors.White);
            }
        }
        #endregion

        #region OnMouseLeftButtonDown
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            OnCalendarDayButtonMouseDown(e);
        }
        #endregion

        #region OnMouseLeftButtonUp
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            OnCalendarDayButtonMouseUp(e);
        }
        #endregion
        #endregion

        #region Methods
        #region DefaultClipPath
        private PathGeometry DefaultClipPath()
        {
            PathGeometry pGeo = new PathGeometry();
            PathFigure pFig = new PathFigure();
            pFig.IsClosed = true;
            pFig.StartPoint = new Point(0, 0);

            LineSegment lSeg1 = new LineSegment();
            LineSegment lSeg2 = new LineSegment();
            LineSegment lSeg3 = new LineSegment();

            lSeg1.Point = new Point(this.ActualWidth, 0);
            lSeg2.Point = new Point(this.ActualWidth, this.ActualHeight);
            lSeg3.Point = new Point(0, this.ActualHeight);            
            pFig.Segments.Add(lSeg1);
            pFig.Segments.Add(lSeg2);
            pFig.Segments.Add(lSeg3);

            pGeo.Figures.Add(pFig);

            return pGeo;
        }
        #endregion

        #region OnCalendarDayButtonMouseDown
        internal void OnCalendarDayButtonMouseDown(MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = this.CalendarDayButtonMouseDown;
            if (null != handler)
            {
                handler(this, e);
            }
        }
        #endregion

        #region OnCalendarDayButtonMouseUp
        internal void OnCalendarDayButtonMouseUp(MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = this.CalendarDayButtonMouseUp;

            if (null != handler)
                handler(this, e);
        }
        #endregion

        #region SendMouseUpEvent
        internal void SendMouseUpEvent(MouseButtonEventArgs e)
        {
            e.Handled = false;
            base.OnMouseLeftButtonUp(e);
        }
        #endregion
        #endregion
    }    
}
